package activity

import (
	"lao-a/helper"
	"lao-a/models/model"
	"net/url"
)

const (
	ACTIVITY_APPLY_READY = iota
	ACTIVITY_APPLY_NORMAL
	ACTIVITY_APPLY_CANCEL
	ACTIVITY_APPLY_EXPIRE
)

// SaveActivityMember 保存活动报名
func (m *DAO) SaveActivityMember(actMem *model.TaActivityMember) (*model.TaActivityMember, error) {
	if actMem.UserName == "" {
		return nil, helper.LogError("请填写活动报名人员姓名")
	}

	if actMem.Phone == "" {
		return nil, helper.LogError("请填写活动报名人员手机")
	}

	if _, err := m.db.Insert(actMem); err != nil {
		return nil, helper.LogError("活动报名失败", err)
	}

	return actMem, nil
}

// UpdateActivityMemStatus 更新活动报名状态
func (m *DAO) UpdateActivityMemStatus(actMem *model.TaActivityMember) error {
	if _, err := m.db.Update(actMem, []string{"status"}, "apply_id=?", actMem.ApplyId); err != nil {
		return helper.LogError("更新活动状态失败", err)
	}

	return nil
}

// UpdateActivityMemID 更新活动报名会员ID
func (m *DAO) UpdateActivityMemID(actMem *model.TaActivityMember) error {
	if _, err := m.db.Update(actMem, []string{"mem_id"}, "apply_id=?", actMem.ApplyId); err != nil {
		return helper.LogError("更新活动会员失败", err)
	}

	return nil
}

// GetActivityMemList 获取活动报名列表
func (m *DAO) GetActivityMemList(params url.Values) ([]model.TaActivityMember, error) {
	var acts []model.TaActivityMember

	if err := m.db.Where("activity_id=?", params.Get("ActivityId")).Find(&acts); err != nil {
		return nil, helper.LogError("获取活动报名列表失败", err)
	}

	return acts, nil
}

// GetActivityMemByID 获取活动报名详情失败
func (m *DAO) GetActivityMemByID(applyId int) (*model.TaActivityMember, error) {
	var act model.TaActivityMember

	if _, err := m.db.Where("apply_id=?", applyId).Get(&act); err != nil {
		return nil, helper.LogError("获取活动报名详情失败", err)
	}

	return &act, nil
}
